package cn.genmer.test.security.machinelearning.tensorflow;

import org.tensorflow.Graph;
import org.tensorflow.Session;
import org.tensorflow.Tensor;

public class TensorFlowExample {
    public static void main(String[] args) {
        // 1.9.0 正常 1.15.0报错 但是不能训练或者训练麻烦
//        System.out.println(TensorFlow.version());
        try (Graph graph = new Graph()) {
            final String value = "Hello, TensorFlow2!";
            try (Tensor<String> tensor = (Tensor<String>) Tensor.create(value.getBytes())) {
                graph.opBuilder("Const", "MyConst")
                        .setAttr("dtype", tensor.dataType())
                        .setAttr("value", tensor)
                        .build();

                graph.opBuilder("Const", "yyq")
                        .setAttr("dtype", tensor.dataType())
                        .setAttr("value", Tensor.create("Genmer".getBytes()))
                        .build();
            }

            try (Session session = new Session(graph)) {
                final Tensor<?> result = session.runner()
                        .fetch("yyq")
                        .run()
                        .get(0);
                System.out.println(new String(result.bytesValue()));
            }
        }
    }
}